package org.dawfinn.droiditarian;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dawfinn.droiditarian.server.data.DeedLocation;
import org.dawfinn.droiditarian.server.data.PMF;

import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class ReportPictureServlet extends HttpServlet {
	private static final String SUCCESS = "success";
	private static final String LOC_KEY = "id";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String key = req.getParameter(LOC_KEY);

		PersistenceManager pm = PMF.get().getPersistenceManager();
		DeedLocation loc = pm.getObjectById(DeedLocation.class, KeyFactory.stringToKey(key));
		Transaction tx = pm.currentTransaction();
		tx.begin();
		loc.setImage(null);
		tx.commit();

		ServletOutputStream out = resp.getOutputStream();
		out.println(SUCCESS);
		out.close();
		pm.close();
	}
}
